package com.zq.vaccinum.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.vaccinum.domain.JsonResult;
import com.zq.vaccinum.domain.entity.User;
import com.zq.vaccinum.domain.entity.UserInfo;
import com.zq.vaccinum.service.IUserInfoService;
import com.zq.vaccinum.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author zq
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/user-info")
public class UserInfoController extends BaseController{
    @Autowired
    IUserInfoService userInfoService;
    @Autowired
    IUserService userService;

    //json解析工具
    ObjectMapper objectMapper = new ObjectMapper();

//    @PreAuthorize("hasAuthority('sys:user-info:list')")
    @RequestMapping("list")
    public JsonResult list(
            @RequestParam(required = false, defaultValue = "") String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "4") Integer pageSize) throws JsonProcessingException {
        return new JsonResult(OK,"查询成功",userInfoService.getAllUserInfoList(pageNum,pageSize));
    }


    //用户账号+信息
    @RequestMapping("insert")
    public String insert(User user, UserInfo userInfo) throws JsonProcessingException {
        // key-vlue的map集合
        HashMap result = new HashMap();
        // 通过业务层添加-user
        boolean save = userService.save(user);
        // 通过业务层添加-userInfo
        userInfo.setUserId(user.getId());
        boolean save1 = userInfoService.save(userInfo);
        //  保存到结果中
        result.put("ok", save && save1);
        //把集合解析为json格式的字符串，返回
        return objectMapper.writeValueAsString(result);
    }


    @RequestMapping("/queryAll")
    public String queryAll() throws JsonProcessingException {
        HashMap hashMap = new HashMap();
        List<UserInfo> list = userInfoService.list();
        hashMap.put("list",list);
        return objectMapper.writeValueAsString(hashMap);
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(int id) throws JsonProcessingException {
        HashMap result = new HashMap();
        boolean b = userInfoService.removeById(id);
        result.put("ok", b);
        return objectMapper.writeValueAsString(result);
    }


    @RequestMapping("/update")
    public String update(UserInfo userInfo) throws JsonProcessingException {
        HashMap result = new HashMap();
        boolean update = userInfoService.updateById(userInfo);
        result.put("ok", update);
        return objectMapper.writeValueAsString(result);
    }

}
